import {Component, OnDestroy, OnInit} from '@angular/core';
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from 'ng-zorro-antd/form';
import {NzInputDirective, NzInputGroupComponent} from 'ng-zorro-antd/input';
import {FormsModule, NonNullableFormBuilder, ReactiveFormsModule, Validators} from '@angular/forms';
import {NzColDirective, NzRowDirective} from 'ng-zorro-antd/grid';
import {NzButtonComponent} from 'ng-zorro-antd/button';
import {NzIconDirective} from 'ng-zorro-antd/icon';
import {NzCheckboxComponent} from 'ng-zorro-antd/checkbox';
import {NzFlexDirective} from 'ng-zorro-antd/flex';
import {DemejimmerApiservice} from '../../api/demejimmer-apiservice.service';
import {Router} from '@angular/router';
import {Demejimmer_constants} from '../../const/demejimmer_constants';
import {AppAuthService} from '../../server/app-auth.service';
import {AppTokenTaskService} from '../../server/app-token-task.service';
import {EventsService} from '../../server/events/Events.service';
import {IEvent} from '../../server/events/EventM';
import {LoginSuccessEvent} from '../../server/events/LoginSuccessEventHandle';

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [
    NzFormItemComponent,
    NzFormLabelComponent,
    NzFormControlComponent,
    NzInputGroupComponent,
    ReactiveFormsModule,
    NzColDirective,
    NzButtonComponent,
    NzIconDirective,
    NzRowDirective,
    FormsModule,
    NzCheckboxComponent,
    NzFlexDirective,
    NzInputDirective,
    NzFormDirective
  ],
  templateUrl: './login.component.html',
  styleUrl: './login.component.less'
})
export class LoginComponent implements OnInit, OnDestroy {
  private gobank: string = "";

  constructor(
    private loginApiservice: DemejimmerApiservice,
    private appAuthService: AppAuthService,
    private appTokenTaskService: AppTokenTaskService,
    private router: Router,
    private fb: NonNullableFormBuilder,
    private appEventsService: EventsService<IEvent>
  ) {
    this.validateForm = this.fb.group({
      email: this.fb.control('123@qq.com', [Validators.required]),
      password: this.fb.control('123', [Validators.required]),
      remember: this.fb.control(true)
    });

    router.routerState.root.queryParams
      .subscribe(params => {
        this.gobank = params[Demejimmer_constants.ReturnUrlKey]; // 这里可以获取到参数值
        console.log("LoginComponent>>>gobank>>>>" + this.gobank);
      })
  }

  ngOnInit(): void {
    //关刷新token任务
    if (this.appTokenTaskService.refreshTokenJobstack) {
      //this.appTokenTaskService.stopRefreshTokenTask();
    }
  }


  ngOnDestroy(): void {
    //开启刷新token任务
    if (!this.appTokenTaskService.refreshTokenJobstack) {
      //this.appTokenTaskService.startRefreshTokenTask()
    }
  }

  validateForm;

  submitForm(): void {
    if (!this.validateForm.valid) {
      console.log('submit', this.validateForm.value);
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({onlySelf: true});
        }
      });
      return;
    }

    if (this.validateForm.valid) {
      console.log('submit', this.validateForm.value);
      this.loginApiservice.login(this.validateForm.controls.email.value, this.validateForm.controls.password.value)
        .subscribe({
          error: (error) => {
            // 处理登录错误
            console.error('Login failed', error);
          },
          next: (response) => {
            console.log("login>response", response);
            this.appAuthService.saveLogin(response.data)

            var loginSuccessEvent = new LoginSuccessEvent(response.data);
            //发布事件消息
            this.appEventsService.broadcastEvent(loginSuccessEvent);

            // 登录成功，跳转到主页
            this.router.navigate(['/home']);
          }
        });

    }
  }


}
